<?php
/** Zend_Controller_Action */
require_once 'Zend/Db.php';
require_once 'Zend/Db/Table.php';
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Session.php';
require_once 'Zend/Session/Namespace.php';
require_once 'Swat/SwatFrame.php';
// -- Swat Navigation
require_once 'Swat/SwatNavBar.php';
require_once 'Swat/SwatNavBarEntry.php';
// -- Swat Message
require_once 'Swat/SwatMessageDisplay.php';
require_once 'Swat/SwatMessage.php';
// -- Swat Form
require_once 'Swat/SwatButton.php';
require_once 'Swat/SwatEntry.php';
require_once 'Swat/SwatFooterFormField.php';
require_once 'Swat/SwatForm.php';
require_once 'Swat/SwatFlydown.php';
require_once 'Swat/SwatOption.php';
require_once 'Swat/SwatOptionControl.php';
require_once 'Swat/SwatPasswordEntry.php';



class Home_CompanyController extends Zend_Controller_Action
{
    public function editAction()
    {
        $registry = new Zend_Controller_Request_Http();
		$session = new Zend_Session_Namespace('User');
		
		// Check for logged in
    	if (!isset($session->company_id)) {
    		$this->_redirect('/index/index');
    	}
    	
    	$nav = new SwatNavBar();
    	$nav->title = 'Navigation Trail';
    	$nav->addEntry(new SwatNavBarEntry('Home', '#'));
    	$nav->addEntry(new SwatNavBarEntry('Company', '#'));
    	$nav->addEntry(new SwatNavBarEntry('Edit'));
    	$this->view->nav = $nav;
    	
    	if($session->saved === true){
    		$this->setNotice('Your information has been saved.');
    		unset($session->saved);
      	}
      	
    	$frame = new SwatFrame();
    	$frame->title = 'Edit Company Profile';
    	$form = new SwatForm('form_edit');
    	$form->action = 'save';
    	
    	$this->connectDatabase();
    	$this->getCompanyMetaData();
    	$this->getCompanyData($session->company_id);
    	
    	$this->parseForm($this->companyMetadata, $this->companyData, $form);

    	$footer = new SwatFooterFormField();
    	$submit = new SwatButton();
    	$submit->title = 'Save Information';
    	
    	$footer->add($submit);
    	$form->add($footer);
    	$frame->add($form);
    	
    	$this->view->company = $this->companyData;
    	$this->view->form = $frame;
    	$this->view->baseUrl = $registry->getBaseUrl();
    	//echo 'Executed in ' . (microtime(true) - Zend_Registry::get('startTime')) . ' seconds';
    	
    }
    
    public function saveAction()
    {
		
		$session = new Zend_Session_Namespace('User');
		
    	$this->connectDatabase();
    	$this->getCompanyData(intval($session->company_id));
    	
    	$db = $this->db;
    	
    	$set_keys = array();
    	$set_values = array();
    	unset($this->companyData['company_id']);
    	foreach($this->companyData as $data_key => $data_value)
    	{
    		if($data_value != $this->_getParam($data_key))
    		{
	    		$set_keys[] = $data_key.' = ?';
	    		$set_values[] = $this->_getParam($data_key);
	    	}
    	}
    	$set_values[] = $session->company_id;
    	
    	if (count($set_keys) > 1) 
    	{
			$sql = 'UPDATE companies SET '.implode(', ', $set_keys).' WHERE company_id = ?';
			$stmt = $db->prepare($sql);
			$stmt->execute($set_values);
		}
		
		$session->saved = true;
		
		$this->_redirect('/home/company/edit');
    }
    
    public function setNotice($message) {
    	$this->view->notice = new SwatMessageDisplay('notice_message');
    	$this->view->notice->add( 
    		new SwatMessage($message,
      		SwatMessage::NOTIFICATION), 1);
    }
    
    public function connectDatabase()
    {
    	$config = Zend_Registry::get('config');
		
		$this->db = Zend_Db::factory($config->database->type, array(
		    'host'     => $config->database->host,
		    'username' => $config->database->username,
		    'password' => $config->database->password,
		    'dbname'   => $config->database->name
		));
    
    }
    
    public function getCompanyData($companyId)
    {
    	$db = $this->db;
		$select = $db->select()
			->from('companies')
			->where('company_id = ?', $companyId);
		$stmt = $select->query();
		$this->companyData = $stmt->fetchAll();
		$this->companyData = $this->companyData[0];
    }
    
    public function getCompanyMetaData()
    {
    	$db = $this->db;
		$select = $db->select()
			->from('metadata')
			->where('table_name = ?', 'companies')
			->order(array('field_order'));
		$stmt = $select->query();
		$this->companyMetadata = $stmt->fetchAll();
    }
    
    public function parseForm ($metadatas, $data, &$form)
    {
        foreach($metadatas as $metadata)
    	{
    		if($metadata['field_type'] == 'input')
    		{
	    		$input = new SwatEntry($metadata['field']);
		    	$input->size = 24;
		    	$input->value = $data[$metadata['field']];
		    	$form->addWithField($input, $metadata['field_title']);
	    	}
    		if($metadata['field_type'] == 'password')
    		{
	    		$input = new SwatPasswordEntry($metadata['field']);
		    	$input->size = 24;
		    	$input->value = $data[$metadata['field']];
		    	$form->addWithField($input, $metadata['field_title']);
	    	}
	    	elseif ($metadata['field_type'] == 'select')
	    	{
	    		$select = new SwatFlydown($metadata['field']);
	    		$options = explode(';', $metadata['field_data']);
	    		foreach($options as $option){
	    			if ($metadata['field_unicode'])
	    			{
	    				$option = '&#' . $option . ';';
	    			}
	    			$select->addOption(new SwatOption($option, $option, 'text/xml'));
	    		}
		    	$select->value = $data[$metadata['field']];
		    	$form->addWithField($select, $metadata['field_title']);
	    	}
    	}
    }
}

?>
